<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区">
        <treeselect v-model="v_campusId" :options="campusOptions" 
                :multiple="false" :disableBranchNodes="true"
                style="width: 200px"
                :select="selectOthers"
                :show-count="true" placeholder="请选择归属校区" />
      </el-form-item>
      <el-form-item label="高职学校" prop="hSchoolId">
        <el-select v-model="queryParams.hSchoolId" placeholder="高职学校">
            <el-option
                v-for="school in hSchoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="本科学校" prop="bSchoolId">
        <el-select v-model="queryParams.bSchoolId" placeholder="本科学校">
            <el-option
                v-for="school in bSchoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:hbSchool:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:hbSchool:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:hbSchool:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:hbSchool:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hbSchoolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="高职学校" align="center" prop="hSchoolName" />
      <el-table-column label="本科学校" align="center" prop="bSchoolName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:hbSchool:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:hbSchool:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改高职对应本科学校关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="校区">
          <treeselect v-model="v_deptId" :options="campusOptions" 
                :multiple="false" :disableBranchNodes="true"
                style="width: 200px"
                :select="selectSchool"
                :show-count="true" placeholder="请选择归属校区" />
      </el-form-item>
        <el-form-item label="高职学校" prop="hSchoolId">
          <el-select v-model="form.hSchoolId" placeholder="高职学校">
            <el-option
                v-for="school in hSchoolOptionsForm"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="本科学校" prop="bSchoolId">
          <el-select v-model="form.bSchoolId" placeholder="高职学校">
            <el-option
                v-for="school in bSchoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHbSchool, getInfoByHBId, getHbSchool, delHbSchool, addHbSchool, updateHbSchool, exportHbSchool } from "@/api/edu/hbSchool";
import { treeselectByType } from "@/api/system/dept";
import { listByDeptId } from "@/api/edu/hSchools";
import { listSchool } from "@/api/edu/bSchools";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "HbSchool",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高职对应本科学校关系表格数据
      hbSchoolList: [],
      campusOptions: [],
      hSchoolOptions: [],
      bSchoolOptions: [],
      hSchoolOptionsForm: [],
      v_campusId: null,
      v_deptId: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hSchoolId: null,
        bSchoolId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hSchoolId: [
          { required: true, message: "高职学校ID不能为空", trigger: "blur" }
        ],
        bSchoolId: [
          { required: true, message: "本科学校ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.queryCampus();
    this.queryBSchool()
  },
  watch: {
    'v_campusId': 'selectOthers',
    'v_deptId': 'selectSchool'
  },
  methods: {
    queryCampus() {
      // 查询校区（deptType = 1）
      treeselectByType("1").then(response => {
        this.campusOptions = response.data;
      })
    },
    queryBSchool() {
      listSchool().then(response => {
        this.bSchoolOptions = response.data
      })
    },
    selectOthers() {
      this.queryHSchool();
    },
    selectSchool() {
      this.queryHSchoolForm();
    },
    queryHSchool() {
      if(this.v_campusId === undefined || this.v_campusId === null) {
        this.$message({
            type: 'warning',
            message: '请选择校区'
          })
          return
      }
      
      listByDeptId(parseInt(this.v_campusId)).then(response => {
        this.hSchoolOptions = response.data
      })
      
    },
    queryHSchoolForm() {
      if(this.v_deptId === undefined || this.v_deptId === null) {
        this.$message({
            type: 'warning',
            message: '请选择校区'
          })
          return
      }
      
      listByDeptId(parseInt(this.v_deptId)).then(response => {
        this.hSchoolOptionsForm = response.data
      })
      
    },
    /** 查询高职对应本科学校关系列表 */
    getList() {
      this.loading = true;
      listHbSchool(this.queryParams).then(response => {
        this.hbSchoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hSchoolId: null,
        bSchoolId: null,
        hSchoolName: null,
        bSchoolName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hSchoolId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加高职对应本科学校关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hSchoolId = row.hSchoolId || this.ids
      getHbSchool(hSchoolId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改高职对应本科学校关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          getInfoByHBId({hSchoolId: this.form.hSchoolId, bSchoolId: this.form.bSchoolId}).then(res => {
            if(res.data > 0) {
              this.msgSuccess("数据已存在");
              return
            } else {
              addHbSchool(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          })
            
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hSchoolIds = row.hSchoolId || this.ids;
      this.$confirm('是否确认删除高职对应本科学校关系编号为"' + hSchoolIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHbSchool(hSchoolIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有高职对应本科学校关系数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportHbSchool(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
